package com.shiguiwu.springmybatis.javabase.nio.selector;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Date;
import java.util.Scanner;

/**
 * @description: selector
 * @author: stone
 * @date: Created by 2021/11/9 10:46
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.nio.selector
 */
public class SelectorClientTests {

    public static void main(String[] args) throws Exception {

        //1创建一个socket通道
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1",8888));

        //2设置非阻塞
        socketChannel.configureBlocking(false);

        //3创建一个buffer
        ByteBuffer buffer = ByteBuffer.allocate(1024);


        //5创建一个Scanner
        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNext()) {
            String next = scanner.next();

            //4 写入buffer数据
            buffer.put((new Date().toString()+"--->"+next).getBytes());

            //5 模式切换
            buffer.flip();

            //6 写入通道
            socketChannel.write(buffer);

            //7 关闭
            buffer.clear();


        }




    }
}
